<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @abstract 网站的请求接口
 * @author zxj
 */
date_default_timezone_set('PRC');
class SitesOrder extends CI_Controller
{
    private $getData; //得到数据

    function __construct()
    {
        parent::__construct();
        $this->getData = gp();
        if ($this->getData) {
            log_output($this->getData);
        }
        $this->getData = $this->getData['data'];
        $this->getData = json_decode($this->getData,true);
        //登录验证
        $this->userSigna($this->getData['user'], $this->getData['key']);
        //加载模型
        $this->load->model('order_model');
        $this->load->model('product_model');
        $this->load->model('material_model');
        $this->load->model('procurement_model');
    }


    /**
     * 验证是否登录
     * @param string $user 用户名
     * @param string $key 密码
     * @return mixed
     */
    function userSigna($user = '', $key = '')
    {
        //输出数据结构
        $where = array('account' => $user, 'password' => md5($key));
        if (!$user or !$key) {
            json_output($this->getData, '用户名密码不能为空.', false);
        } else {
            $userFirst = $this->common_model->selectFirst('user', $where);
            if (!$userFirst) {
                json_output(array('userFirst' => $userFirst, 'key' => $key, 'user' => $user), '帐号密码错误.', false);
            }
        }
        return true;
    }




    /***
     * 接受报价单数据
     */
    function get_priceList()
    {
        $sitesList = $this->getData['maintaininfo_ids'];
        if (!$sitesList || !is_array($sitesList)) {
            json_output($this->getData, '数据为空或不是数组', false);
        }

        foreach ($sitesList as $key => $rs) {
            $maintaininfo_id = $rs['maintaininfo_id'];
            $is_accept_price = $rs['agree_status'];
            //先获取质检报告
            $priceFirst = $this->common_model->selectFirst('maintain_qcreport', array('maintaininfo_id' => $maintaininfo_id));
            if ($priceFirst) {
                $setData = array(
                    'is_accept_price' => (int)$is_accept_price,
                    'second_order_id' => $this->getData['second_order_id'],
                    'order_info' => $this->getData['order_info']
                );
                $where = array('pid' => $priceFirst['pid']);
                $this->common_model->update('maintain_product_price', $setData, $where);

                $is_a_p = (int)$is_accept_price;
                if ($is_a_p == 1) {
                    $sss = $this->procurement_model->ssslele($priceFirst['pid']);
                    $idlist = array();
                    foreach ($sss as $val) {
                        $idlist[] = $val['id'];
                        if($val['sku']!=''){
                            $this->procurement_model->updatecg($val['id'], array('status' => 2));
                        }
                    }
                    $url = "http://58.250.165.205:8889/webService/GetPublicInventoryRequirement.php";
                    $this->materialpost($url, $idlist);
                    //更新报价单物料费用
                    $accSum_price = 0;
                    $wuliaoList = $this->material_model->get_wuliao('', $priceFirst['pid']);
                    foreach ($wuliaoList as $rs) {
                        $accSum_price += intval($rs['accessory_quantity']) * floatval($rs['accessory_price']);
                    }
                    $cpPriceData['accessory_price'] = $accSum_price;
                    $cpPriceData['pid'] = $priceFirst['pid'];
                    $cpPriceData['oid'] = $priceFirst['oid'];
                    $this->product_model->setProductPrice($priceFirst['pid'], $cpPriceData);
                }
            } else {
                json_output('', '报价单不存在');
            }
        }
        json_output('', '接受数据成功!', true);

    }

    public function materialpost($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_exec($ch);
        curl_close($ch);
    }
}
